package com.ynkbny.tasks;

import com.baomidou.mybatisplus.extension.service.IService;
import com.ynkbny.aspect.TaskOperationLog;
import com.ynkbny.domain.pojo.EnterpriseGetNameVO;
import com.ynkbny.service.*;
import lombok.var;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * @Author YY
 * @Date 2023/11/27
 */
@Component
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
public class SynchronizeBasicEnterpriseData implements BaseJob {

    private static final Logger log = LoggerFactory.getLogger(SynchronizeBasicEnterpriseData.class);

    @Resource
    private EnterpriseService enterpriseService;

    @Resource
    private TokenService tokenService;

    @Resource
    private ProcessConfigureService processConfigureService;

    @Resource
    private ContacterService contacterService;

    @Resource
    private CalculaterService calculaterService;

    @Resource
    private CertificationService certificationService;

    @Resource
    private DeviceconfigureService deviceconfigureService;

    @Resource
    private EgaccountService egaccountService;

    @Resource
    private EgconservationService egconservationService;

    @Resource
    private EnergyMonitorService energyMonitorService;

    @Resource
    private GaugeconfigureService gaugeconfigureService;

    @Resource
    private GaugereplacecfgService gaugereplacecfgService;

    @Resource
    private GaugeverifycfgService gaugeverifycfgService;

    @Resource
    private ManagerService managerService;

    @Resource
    private ManageSystemService manageSystemService;

    @Resource
    private MaterielProductService materielProductService;

    @Resource
    private OutputvalueService outputvalueService;

    @Resource
    private ProductstructService productstructService;

    @Resource
    private UnitconfigureService unitconfigureService;

    @Resource
    private TEpDataCollectConfigureService dataCollectConfigureService;

    /**
     * 同步企业基础数据
     */
    public void synchronizeBasicEnterpriseData(EnterpriseGetNameVO enterpriseGetNameVO) {
        List<IService<? extends Serializable>> list = Arrays.asList(processConfigureService
                , contacterService
                , calculaterService
                , certificationService
                , deviceconfigureService
                , egaccountService
                , egconservationService
                , energyMonitorService
                , gaugeconfigureService
                , gaugereplacecfgService
                , gaugeverifycfgService
                , managerService
                , manageSystemService
                , materielProductService
                , outputvalueService
                , productstructService
                , unitconfigureService
//                , dataCollectConfigureService //采集配置项需要单独获取
        );

        for (IService<? extends Serializable> iService : list) {
            Class<? extends IService> aClass = iService.getClass();
            log.info("同步企业基础数据:{}", aClass);
            try {
                aClass.getDeclaredMethod("getDataForLaoGou", String.class, EnterpriseGetNameVO.class)
                        .invoke(iService, tokenService.getLaoGouWebToken(), enterpriseGetNameVO);
            } catch (Exception e) {
                log.info("同步企业基础数据异常", e);
            }

        }

    }

    @Override
    @TaskOperationLog(description = "同步企业基础数据(参数例:企业社会信用代码1,企业社会信用代码2)；无参则同步所有企业")
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //传递的参数
        JobDataMap data = jobExecutionContext.getTrigger().getJobDataMap();
        String param = data.getString("invokeParam");

        // 获取所有企业的社会信用代码列表
        List<EnterpriseGetNameVO> enterprise = enterpriseService.getNameList();
        // 将企业社会信用代码列表转换为map
        var map = new HashMap<String, EnterpriseGetNameVO>();
        enterprise.forEach(n -> map.put(n.getEnterpriseCode(), n));

        // 无参则同步所有企业
        if (param == null || param.isEmpty()) {
            map.forEach((k, v) -> {
                try {
                    this.synchronizeBasicEnterpriseData(v);
                } catch (Exception e) {
                    log.info("同步企业基础数据异常:" + v.toString(), e.getMessage());
                }
            });
        } else {
            // 获取需要同步数据的企业社会信用代码列表
            String[] split = param.trim().split(",");
            for (String s : split) {
                if (map.containsKey(s)) {
                    EnterpriseGetNameVO enterpriseGetNameVO = map.get(s);
                    try {
                        this.synchronizeBasicEnterpriseData(enterpriseGetNameVO);
                    } catch (Exception e) {
                        log.info("同步企业基础数据异常:" + enterpriseGetNameVO.toString(), e.getMessage());
                    }
                }
            }
        }
    }
}
